package CutLoad.components.container
{
	import CutLoad.Base;
	import CutLoad.components.CRender;
	import CutLoad.cookies.Fun;
	import CutLoad.cookies.Painter;
	import CutLoad.cookies.RandNO;
	
	import flash.display.Sprite;
	/**
	 * 可变尺寸的布局容器,添加在里面每一个元素都会和该容器等宽高。  当它改变尺寸时，会调用每一个元素的setWH()方法
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */
	public class ContainerS extends Base
	{
		private var arrSize:Array=new Array();
		//外观尺寸
		private var displayWidth:Number=0,displayHeight:Number=0;
		//
		private var spGuide:Sprite;
		/**
		 *  放置子元素
		 */		
		protected var spContent:Sprite;
		//
		/**
		 * 背景
		 */		
		protected var bakRender:CRender;
		//当前显示，上一个显示的
		private var index:Number=-1
		public function ContainerS()
		{
			spContent=new Sprite();
			bakRender=new CRender();
			bakRender.init(0,0,{color:0xffffff,alpha:0,isBitmap:false},1,1);
			this.addChild(bakRender);
			this.addChild(spContent);
		}
		/**
		 * 添加一个元素
		 * @param base
		 * @param isShow
		 * @return 
		 * 
		 */		
		public function append(base:Base,isShow:Boolean=true):Number{
			arrSize.push(base);
			var returnIndex:Number=arrSize.length-1;
			if(isShow){
				if(this.index!=-1) arrSize[this.index].visible=false;
				base.visible=true;
				this.index=returnIndex
			}else{
				base.visible=false;
			}
			spContent.addChild(base);
			return returnIndex;
		}
		/**
		 * 获得子元素的父容器 
		 * @return 
		 * 
		 */		
		public function getChildeParent():Sprite{
			return spContent;
		}
		/**
		 * 刷新画面 
		 * 
		 */		
		public function update():void{
			for(var i:Number=0;i<arrSize.length;i++){
				arrSize[i].setWH(getShowWidth(),getShowHeight());
			}
		}
		/**
		 * 显示附助边框
		 * 
		 */		
		public function showGuide():void{
			if(spGuide!=null) this.removeChild(spGuide);
			spGuide=Painter.getRect(this.getShowWidth(),this.getShowHeight(),0x000000,0,0,0,2,RandNO.getRndColor32());
			spGuide.mouseChildren=false;
			spGuide.mouseEnabled=false;
			spGuide.buttonMode=false;
			this.addChild(spGuide);
		}
		/**
		 * 清除所有元素 
		 * 
		 */		
		public function removeAll():void{
			this.removeChild(spContent);
			spContent=new Sprite();
			this.addChild(spContent);
			this.swapChildren(spContent,bakRender);
			arrSize=new Array();
			index=-1
		}
		
		
		/**
		 * 设置背景
		 *
		 */			
		public function setBak(way:uint,type:uint,config:Object):void{
			clrBak()
			bakRender=new CRender();
			bakRender.init(way,type,config,this.getShowWidth(),this.getShowHeight());
			this.addChild(bakRender);
			this.swapChildren(spContent,bakRender);
		}
		//
		/**
		 * 设置一个色块的背景
		 * @param c 颜色
		 * @param a 透明度
		 * 
		 */		
		public function setBakColor(c:Number=0xffffff,a:Number=0.5):void{
			clrBak()
			bakRender=new CRender();
			bakRender.init(0,0,{color:0xffffff,alpha:1,isBitmap:false},this.getShowWidth(),this.getShowHeight());
			this.addChild(bakRender);
			this.swapChildren(spContent,bakRender);
			
		}
		/**
		 * 清除背景 
		 * @return 
		 * 
		 */		
		public function clrBak():void{
			if(bakRender!=null) this.removeChild(bakRender);
		}
		/**
		 * 更新背景 
		 * 
		 */		
		private function updateBak():void{
			if(bakRender!=null) bakRender.setWH(this.getShowWidth(),this.getShowHeight());
		}
		/**
		 * 某一个显示出来 
		 * @param index
		 * @return 
		 * 
		 */		
		public function showOne(index:Number):void{
			if(this.index!=-1) arrSize[this.index].visible=false;
			this.index=index;
			arrSize[this.index].visible=true;
		}
		override public function setWH(w:Number=0, h:Number=0):void{
			displayWidth=w;
			displayHeight=h;
			update()
			//更新背景
			updateBak()
		}
		override public function getShowWidth():Number{
			return displayWidth;
		}
		override public function getShowHeight():Number{
			return displayHeight;
		}
	}
}